import java.util.ArrayList;

//
//  @ Project : Project1
//  @ File Name : Party.java
//  @ Date : 1/21/2013
//  @ Author : Graham Tapscott
//
//

/**
 * Party - this class represents a party of creatures.
 * @author gtapscott
 *
 */
public class Party implements Comparable<Party>{

	// Instance variables
	private Integer index;
	private String location;
	private ArrayList<Creature> creatureList;
	private String name;

	/**
	 * No-arg constructor
	 */
	public Party(){
		creatureList = new ArrayList<Creature>();		
	}

	/**
	 * Party - argumented constructor, this is to be used only as a comparison functions
	 * @param idx
	 */
	public Party(int idx) {
		index = idx;
	}	

	/**
	 * Party - argumented constructor, this can be used to instantiate a new object from a 
	 * string representation of the object
	 * @param input a string representation of the object
	 */
	public Party(String[] input) {
		this();
		this.deSerialize(input);
	}

	/**
	 * getIndex - index variable getter
	 * @return index variable value
	 */
	public Integer getIndex() {
		return index;	
	}

	/**
	 * getName - name variable getter
	 * @return name variable value
	 */
	public String getName() {
		return name;	
	}

	/**
	 * getLocation - location variable getter
	 * @return location variable value
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * setLocation - location variable setter
	 * @param location value to set location variable to
	 */
	public void setLocation(String location) {
		this.location = location;
	}	

	/**
	 * setIndex - index variable setter
	 * @param idx value to set index variable to
	 */
	public void setIndex(int idx) {
		index = idx;		
	}	

	/**
	 * addCreature - adds a creature to the creatureList
	 * @param aCreature a creature to add to the creatureList
	 */
	public void addCreature(Creature aCreature){
		creatureList.add(aCreature);
	}

	public String toString() {
		return serialize();
	}

	/**
	 * dumpObject - dumps this object and all subobjects in the creatureList to a string
	 * representation
	 * @return a string representation of this and all subobjects
	 */
	public String dumpObject() {
		String result = this.serialize() + Utils.newLine;
		for(int i=0;i<creatureList.size();i++){
			result += Utils.tabChar + creatureList.get(i).dumpObject() + Utils.newLine;
		}
		return result;
	}

	/***
	 * serialize - serializes an object 
	 * @return a string representing the object
	 */
	private String serialize() {
		String output = "p:" + this.index + ":" + this.name;
		return output;
	}

	/***
	 * deSerialize - deserializes an object
	 * @param item a string representing the object to deserialize.
	 */	
	public void deSerialize(String[] input){
		this.index = Integer.parseInt(input[1].trim());
		this.name = input[2].trim();
	}

	public int compareTo(Party otherParty) {
		if(this.index > otherParty.getIndex())
			return 1;
		else
			if(this.index < otherParty.getIndex())
				return -1;
			else
				return 0;

	}
}

